using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint.WebControls;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using System.IO;

namespace PhotoFieldControl
{
   public class PhotoFieldEditor: UserControl, IFieldEditor
    {
       protected DropDownList drpdwnAvailablePictureLists;
       protected TextBox txtUrlWeb;
       protected CheckBox chkUseThisWeb;
       protected Button btnConnectWeb;
       protected Literal litBreak;

       public PhotoFieldEditor()
       {
           //if (ViewState["New"] == null)
           //{
           //    ViewState["New"] = 0;
           //}
           //else
           //{
           //    ViewState["New"] = ((int)  ViewState["New"]) + 1;
           //}
       }

       protected override void OnInit(EventArgs e)
       {
           base.OnInit(e);
           chkUseThisWeb.AutoPostBack = true;

           chkUseThisWeb.CheckedChanged += new EventHandler(chkUseThisWeb_CheckedChanged);
           btnConnectWeb.Click += new EventHandler(btnConnectWeb_Click);
       }

       void btnConnectWeb_Click(object sender, EventArgs e)
       {
           try
           {
               SPSite site = new SPSite(txtUrlWeb.Text);
               litBreak.Text = "<br/>";
               if (site != null)
               {
                   SPWeb web = site.OpenWeb();
                   FillDropDown(web);
               }
           }
           catch (FileNotFoundException)
           {
               litBreak.Text = "<br/><FONT style=\"color:red;\">Impossible to connect to this site!</FONT><br/>";
               drpdwnAvailablePictureLists.Items.Clear();
           }
           catch(UriFormatException)
           {
               litBreak.Text = "<br/><FONT style=\"color:red;\">The format of the URL is invalid!</FONT><br/>";
               drpdwnAvailablePictureLists.Items.Clear();
           }
       }

       void chkUseThisWeb_CheckedChanged(object sender, EventArgs e)
       {
           if (!chkUseThisWeb.Checked)
           {
               //txtUrlWeb.Enabled = true;
               //btnConnectWeb.Visible = true;
               //litBreak.Text = "<br/>";
               
               UpdateFields(true);
               drpdwnAvailablePictureLists.Items.Clear();
           }
           else
           {
               UpdateFields(false);
           }
       }

       void UpdateFields(bool ShowEnabled)
       {
           txtUrlWeb.Enabled = ShowEnabled;
           btnConnectWeb.Visible = ShowEnabled;
           
           if(ShowEnabled)
            litBreak.Text = "<br/>";
           else
            litBreak.Text = "";

           FillDropDown(SPContext.Current.Web);
       }

        public void InitializeWithField(SPField field)
        {
            EnsureChildControls();
            PhotoField myField = field as PhotoField;

            if (myField != null && !IsPostBack)
            {                
                txtUrlWeb.Text = myField.WebUrl;
                chkUseThisWeb.Checked = myField.UseThisWeb;
                UpdateFields(!chkUseThisWeb.Checked);
                drpdwnAvailablePictureLists.SelectedValue = myField.ListName;
            }

        }

        public void OnSaveChange(SPField field, bool isNew)
        {


            PhotoField myField = field as PhotoField;
            

            if (isNew)
            {
                FieldProps fp = new FieldProps();
                fp.UseThisWeb = chkUseThisWeb.Checked;
                fp.WebUrl = txtUrlWeb.Text;
                fp.ListName = drpdwnAvailablePictureLists.SelectedValue;
                myField.UpdateApplicationProperty(fp);
            }
            else
            {                 
                myField.UseThisWeb = chkUseThisWeb.Checked;
                myField.WebUrl = txtUrlWeb.Text;
                myField.ListName = drpdwnAvailablePictureLists.SelectedValue;
            }


        }

        public bool DisplayAsNewSection
        {

            get
            {

                return true;

            }

        }



        protected override void CreateChildControls()
        {

            base.CreateChildControls();



            

            if (chkUseThisWeb.Checked)
            {

                SPWeb web = SPContext.Current.Web;

                FillDropDown(web);
            }

        }

       private void FillDropDown(SPWeb web)
       {
           drpdwnAvailablePictureLists.Items.Clear();
           drpdwnAvailablePictureLists.Items.Add("None");
           foreach (SPList list in web.Lists)
           {
               if (list.GetType().Equals(typeof(SPPictureLibrary)))
                   drpdwnAvailablePictureLists.Items.Add(new ListItem(list.Title));
           }
       }



        
    }
}
